/**
 * Created by jack on 18/4/1.
 */

/*
given a tree like this one;

         1
        / \
       2   5
      / \   \
     3   4   6

then flatten it to

       1
        \
         2
          \
           3
            \
             4
              \
               5
                \
                 6

basic recursive program;

这里需要考虑的问题，因为这里的TreeNode传入的不是引用而是对应的地址，
所以对于这里的root的操作对于团来的root是没有任何改变的
 */

public class FlattenTree {
    private TreeNode current;
    public void flatten(TreeNode root) {
        if(root == null){
            return ;
        }

        current = root;
        TreeNode rootleft = root.left;
        TreeNode rootright = root.right;
        root.left = null;
        root.right = null;
        if(rootleft != null){
            addson(rootleft);
        }
        if(rootright != null){
            addson(rootright);
        }
    }
    private void addson(TreeNode node){
            current.right = new TreeNode(node.val);
            current = current.right;
            if(node.left != null){
                addson(node.left);
            }
            if(node.right != null){
                addson(node.right);
            }
    }
}
